{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "https://langchain-ai.github.io/langgraph/how-tos/map-reduce/\n",
    "\n",
    "生成关于topic的N个subjects，N个subjects并行执行生成一个joke，最终从所有joke中选择最好的一个。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cec430bf5216d6f0"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "from langgraph.constants import START, END\n",
    "from langgraph.graph import StateGraph\n",
    "from langgraph.types import Send\n",
    "from typing import Annotated\n",
    "import operator\n",
    "from typing_extensions import TypedDict\n",
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from pydantic import BaseModel, Field\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "# Model and prompts\n",
    "# Define model and prompts we will use\n",
    "subjects_prompt = \"\"\"Generate a comma separated list of between 2 and 5 examples related to: {topic}.\"\"\"\n",
    "joke_prompt = \"\"\"Generate a joke about {subject}\"\"\"\n",
    "best_joke_prompt = \"\"\"Below are a bunch of jokes about {topic}. Select the best one! Return the ID of the best one.\n",
    "\n",
    "{jokes}\"\"\"\n",
    "\n",
    "class Subjects(BaseModel):\n",
    "    subjects:list[str]\n",
    "    \n",
    "class Joke(BaseModel):\n",
    "    joke:str\n",
    "    \n",
    "class BestJoke(BaseModel):\n",
    "    id:int = Field(description=\"Index of the best joke, starting with 0\",ge=0)\n",
    "    \n",
    "    \n",
    "llm = ChatOpenAI(\n",
    "    openai_api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model_name=\"qwen-max\",\n",
    "    temperature=0, streaming=True,\n",
    ")\n",
    "\n",
    "\n",
    "# This will be the overall state of the main graph.\n",
    "# It will contain a topic (which we expect the user to provide)\n",
    "# and then will generate a list of subjects, and then a joke for\n",
    "# each subject\n",
    "class OverallState(TypedDict):\n",
    "    topic:str\n",
    "    subjects:list\n",
    "    # Notice here we use the operator.add\n",
    "    # This is because we want combine all the jokes we generate\n",
    "    # from individual nodes back into one list - this is essentially\n",
    "    # the \"reduce\" part\n",
    "    jokes:Annotated[list,operator.add]\n",
    "    best_selected_joke:str\n",
    "    \n",
    "# This will be the state of the node that we will \"map\" all\n",
    "# subjects to in order to generate a joke\n",
    "class JokeState(TypedDict):\n",
    "    subject:str\n",
    "    \n",
    "# This is the function we will use to generate the subjects of the jokes\n",
    "def generate_topics(state: OverallState):\n",
    "    prompt = subjects_prompt.format(topic=state[\"topic\"])\n",
    "    response = llm.with_structured_output(Subjects).invoke(prompt)\n",
    "    return {\"subjects\": response.subjects}\n",
    "\n",
    "# Here we generate a joke, given a subject\n",
    "def generate_joke(state: JokeState):\n",
    "    prompt = joke_prompt.format(subject=state[\"subject\"])\n",
    "    response = llm.with_structured_output(Joke).invoke(prompt)\n",
    "    return {\"jokes\": [response.joke]}\n",
    "\n",
    "# Here we define the logic to map out over the generated subjects\n",
    "# We will use this an edge in the graph 将topic分发到generate_joke\n",
    "def continue_to_jokes(state: OverallState):\n",
    "    # We will return a list of `Send` objects\n",
    "    # Each `Send` object consists of the name of a node in the graph\n",
    "    # as well as the state to send to that node\n",
    "    print(f\"Continue to joke {state['subjects']}\")\n",
    "    return [Send(\"generate_joke\", {\"subject\": s}) for s in state[\"subjects\"]]\n",
    "\n",
    "# Here we will judge the best joke\n",
    "def best_joke(state: OverallState):\n",
    "    jokes = \"\\n\\n\".join(state[\"jokes\"])\n",
    "    prompt = best_joke_prompt.format(topic=state[\"topic\"], jokes=jokes)\n",
    "    response = llm.with_structured_output(BestJoke).invoke(prompt)\n",
    "    return {\"best_selected_joke\": state[\"jokes\"][response.id]}\n",
    "\n",
    "# Construct the graph: here we put everything together to construct our graph\n",
    "graph = StateGraph(OverallState)\n",
    "graph.add_node(\"generate_topics\", generate_topics)\n",
    "graph.add_node(\"generate_joke\", generate_joke)\n",
    "graph.add_node(\"best_joke\", best_joke)\n",
    "\n",
    "graph.add_edge(START, \"generate_topics\")\n",
    "graph.add_conditional_edges(\"generate_topics\", continue_to_jokes, [\"generate_joke\"])\n",
    "graph.add_edge(\"generate_joke\", \"best_joke\")\n",
    "graph.add_edge(\"best_joke\", END)\n",
    "app = graph.compile()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T07:50:10.456535Z",
     "start_time": "2024-11-04T07:50:10.357395Z"
    }
   },
   "id": "ec7073071cb3be14",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADb/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsN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dCQenhruj0NHmJsTYrSa13zT1WjfFG9RaTna/epl17VRe/Tna/epl17VRe/XfU7UeKPco3aLSc7X71MuvaqL36c7X71MuvaqL36anajxR7lG7RaTna/epl17VRe/Tna/epl17VRe/TU7UeKPco3aLSc7X71MuvaqL36c7X71MuvaqL36anajxR7lG7RTFmyy7X60UVzo8NvJpKyFlRCZpKWJ5Y5oc3eY+YOadD8FwBHQQFmc7X71MuvaqL36anajxR7lG7RaTna/epl17VRe/Tna/epl17VRe/TU7UeKPco3aLSc7X71MuvaqL36c7X71MuvaqL36anajxR7lG7RaTna/epl17VRe/Tna/epl17VRe/TU7UeKPco3ax9lvxIof7Wo/fyLXNq8krTyUGMT0EjuAqLhVQGJn+8RFI9ztPMANejUdIq8es0ePWWjt0T3Stp2bpkf8ACe7pc4/OSSfzrjfzFm61KxWZid0xPCuHecIbFERecqIiICIiAiIgIiICIiCf2fTiqwTHZhPcaoSW+nfy94j5Otk1jad6dug3ZD0uGg0OqoFObN6gVez3GZ21dfcGyW2neKu6s3KubWNp35m9Uh6XDqJKo0BERAREQEREBERAREQEREBERAREQEREBERBObN6xlx2e4zVR19ZdI57bTyNrrjHydTUAxtIklb/AFXu6SOokqjU7s6recsAxur79rLly9up5e/LhCIaifWNp35WDQNeekt6iSFRICIiAiIgIiICIiAiIgIiICKan2l4lTSujlya0Me0kFprY9QegjpX5+NLDvWm0dtj9q0bPfT/AKTlKaTgqUUt40sO9abR22P2p40sO9abR22P2ps991JylOrOCpRS3jSw71ptHbY/anjSw71ptHbY/amz33UnKTVnBUrVZHldkw+ijrL9eLfZKSSQQsqLjVMp43PIJDA55ALtGuOnToD5lq/Glh3rTaO2x+1ca7rmzYbt42H3rH4MltD7xSkXK1/jsf8ArUbXbrfhf1muez/n16k2e+6k5Sas4On7INpOLZpiljprLl9Jk1e22QzS79VG6uc3caDJPE1xcx2rhvAjg52iv14t/wAnzhWMbFtltReMgvNsoMsyGQSVFPU1MbJqWnYSIonAnVpJ3nkf7zQeLV6p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ieDVo9FUXZ2exW/Xbv/nOf4KQ8wc70Py2n+1b7U53ofltP9q32r0/4NWj0VRdnZ7E8GrR6Kouzs9ifrt3/AM5z/BSHmDneh+W0/wBq32o27UckrYoqmOeZxAbFAeUe4+YNbqT/AHL0/wCDVo9FUXZ2exZVLQUtCCKamhpweqJgb/gon49Ypuu5z/BSHHcD2WVd5qYbhf6V1HbYy2SO3zf6WocDqDKP6rOjyDxd0OAALXdqRF85pWl3ml29e8+kdEAiIsQIiICIiD//2Q==",
      "text/plain": "<IPython.core.display.Image object>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Image\n",
    "\n",
    "Image(app.get_graph().draw_mermaid_png())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T07:50:11.261007Z",
     "start_time": "2024-11-04T07:50:10.461846Z"
    }
   },
   "id": "e49e774748c2fd68",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Continue to joke ['lion', 'elephant', 'giraffe', 'zebra']\n",
      "{'generate_topics': {'subjects': ['lion', 'elephant', 'giraffe', 'zebra']}}\n",
      "{'generate_joke': {'jokes': [\"Why don't lions play cards in the jungle? Because there are too many cheetahs!\"]}}\n",
      "{'generate_joke': {'jokes': ['Why did the zebra get a ticket? Because he was caught jaywalking!']}}\n",
      "{'generate_joke': {'jokes': [\"Why don't elephants use computers? They're afraid of mice!\"]}}\n",
      "{'generate_joke': {'jokes': ['Why did the giraffe need a long neck? Because it has a lot of books on its reading list and needs to reach the top shelf!']}}\n",
      "{'best_joke': {'best_selected_joke': \"Why don't elephants use computers? They're afraid of mice!\"}}\n"
     ]
    }
   ],
   "source": [
    "# Call the graph: here we call it to generate a list of jokes\n",
    "for s in app.stream({\"topic\": \"animals\"}):\n",
    "    print(s)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T07:50:18.509942Z",
     "start_time": "2024-11-04T07:50:11.263847Z"
    }
   },
   "id": "ca8d221b3dd125c7",
   "execution_count": 23
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
